package com.macro.mall.portal.controller;


import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.portal.domain.Appraisal;
import com.macro.mall.portal.domain.User;
import com.macro.mall.portal.domain.UserIncome;
import com.macro.mall.portal.domain.vo.MsgVo;
import com.macro.mall.portal.domain.vo.PageVo;
import com.macro.mall.portal.domain.vo.TotalVo;
import com.macro.mall.portal.service.IUserService;
import com.macro.mall.portal.util.ConcurrentHashMapCacheUtils;
import com.macro.mall.portal.util.SendMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/users")
@Api(tags = "用户模块", description = "用户控制器")
public class UserController {

    @Autowired
    private IUserService userService;

    private static Map mapMsg = new HashMap();

    @ApiOperation("用户注册")
    @RequestMapping(value = "/registered", method = RequestMethod.POST)
    public CommonResult registered(@RequestBody User user) {
        try {
            userService.registered(user);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success(null, "添加成功");
    }

    @ApiOperation("用户信息查询")
    @RequestMapping(value = "/userInfo", method = RequestMethod.POST)
    public CommonResult userInfo(@RequestBody User a) {
        try {
            return CommonResult.success(userService.userInfos(a));
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }

    }

    @ApiOperation("云转介权限查询")
    @RequestMapping(value = "/superInfo", method = RequestMethod.POST)
    public CommonResult superInfo(String shareCode) {
        User user = null;
        try {
            user = userService.userInfo(shareCode);
            if (user.getPhone() == null || user.getPhone() == "") {
                return CommonResult.error(203, "请绑定手机号!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success(user);
    }

    @ApiOperation("专属二维码")
    @RequestMapping(value = "/qrCode", method = RequestMethod.POST)
    public CommonResult qrCode(@RequestBody User user) {
        CommonResult qrCode = null;
        try {
            qrCode = userService.qrCode(user);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success(qrCode);
    }

    @ApiOperation("修改资料，根据手机号码绑定用户信息")
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public CommonResult bind(@RequestBody User user) {
        try {
            userService.bind(user);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success("修改成功");
    }

    @ApiOperation("根据手机号码绑定个人信息")
    @RequestMapping(value = "/bindUser", method = RequestMethod.POST)
    public CommonResult bindUser(@RequestBody User vxUser) {
        try {
            userService.bindUser(vxUser);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success("修改成功");
    }





    @ApiOperation("绑定银行卡信息")
    @RequestMapping(value = "/bank", method = RequestMethod.POST)
    public CommonResult bank(@RequestBody User user) {
        if (user.getOpenId() == null) {
            return CommonResult.failed("请重试");
        }
        try {
            userService.bank(user);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success("", "修改成功");
    }

    @ApiOperation("发送验证码")
    @RequestMapping(value = "/sendMsg", method = RequestMethod.POST)
    public CommonResult sendMsg(@RequestBody MsgVo msgVo) {
        if (StringUtils.isEmpty(msgVo.getPhone())) {
            return CommonResult.failed("电话号码为空");
        }
        try {
            String msg = SendMsg.send(msgVo.getPhone());
            ConcurrentHashMapCacheUtils.setCache(msgVo.getPhone(), msg, 60 * 5000L);
            return CommonResult.success(null, "发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("网络异常");
        }
    }

    @ApiOperation("校验验证码")
    @RequestMapping(value = "/checkMsg", method = RequestMethod.POST)
    public CommonResult checkMsg(@RequestBody MsgVo msgVo) {
        if (StringUtils.isEmpty(msgVo.getPhone()) || StringUtils.isEmpty(msgVo.getCode())) {
            return CommonResult.failed("电话号码为空或者验证码为空");
        }
        try {
            String code = (String) ConcurrentHashMapCacheUtils.getCache(msgVo.getPhone());
            if (!StringUtils.isEmpty(code) && code.equals(msgVo.getCode())) {
                ConcurrentHashMapCacheUtils.deleteCache(msgVo.getPhone());
                return CommonResult.success(null, "校验成功");
            } else if (StringUtils.isEmpty(code) || !code.equals(msgVo.getCode())) {
                return CommonResult.failed("校验失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("网络异常");
        }
        return CommonResult.success("网络异常");
    }


    @ApiOperation("用户查询合伙人收益")
    @RequestMapping(value = "/getIncome", method = RequestMethod.POST)
    public CommonResult getIncome(@RequestBody PageVo page) {
        try {
            CommonResult income = userService.getIncome(page);
            return income;
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("合伙人分页查询收益")
    @RequestMapping(value = "/getPageListIncome", method = RequestMethod.POST)
    public CommonResult getPageListIncome(@RequestBody PageVo page) {
        try {
            PageInfo<UserIncome> pageListIncome = userService.getPageListIncome(page);
            return CommonResult.success(pageListIncome);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("数据统计收益")
    @RequestMapping(value = "/getDIncome", method = RequestMethod.POST)
    public CommonResult getDIncome(@RequestBody TotalVo vo) {
        try {
            CommonResult commonResult = userService.getDIncome(vo);
            return commonResult;
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("数据统计收益-合伙人分页")
    @RequestMapping(value = "/getDInfo", method = RequestMethod.POST)
    public CommonResult getDInfo(@RequestBody PageVo vo) {
        try {
            CommonResult commonResult = userService.getDInfo(vo);
            return commonResult;
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

}
